package com.facebook.games.fa.components
{
	import com.facebook.games.fa.events.AttributeEvent;
	import com.facebook.games.fa.model.Attribute;
	import com.facebook.games.fa.model.Card;
	import com.facebook.games.fa.skins.AttributeSkin;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.ChangeWatcher;
	
	import spark.components.Label;
	import spark.components.RichText;
	import spark.components.SkinnableContainer;

	[Event(name="attributeSelected", type="com.facebook.games.fa.events.AttributeEvent")]
	
	/**
	 * Atributo
	 * @author Diego Maehler <dmaehler@gmail.com>
	 */
	public class Attribute extends SkinnableContainer
	{
	
		private var _attribute : com.facebook.games.fa.model.Attribute = new com.facebook.games.fa.model.Attribute();
		
		private var valueWatcher : ChangeWatcher;
		
		[Bindable]
		public var card : Card;
		
		[SkinPart]
		public var labelName : Label;
		
		[SkinPart]
		public var labelValue : RichText;
		
		[Bindable]
		public function set attribute (att : com.facebook.games.fa.model.Attribute) : void
		{
			this._attribute = att;
			this.addValueWatcher();
			this.refreshLabels();
		}
		
		public function get attribute () : com.facebook.games.fa.model.Attribute
		{
			return this._attribute;
		}
		
		/**
		 * 
		 */
		public function Attribute()
		{
			super();
			
			this.useHandCursor = true;
			this.mouseChildren = false;
			this.setStyle("skinClass", AttributeSkin);
			
			this.addEventListener(MouseEvent.CLICK, this.mouseClickHandler);
			
		}
		
		/**
		 * ClickHandler
		 * @param event
		 */
		protected function mouseClickHandler(event:MouseEvent):void
		{
			dispatchEvent(new AttributeEvent(AttributeEvent.ATTRIBUTE_SELECTED, this.attribute));
		}
		
		/**
		 * Atualiza os labels
		 */
		protected function refreshLabels () : void
		{
			if (this.labelName != null && this.labelValue != null) {
				if (this._attribute != null && this.card != null) {
					this.labelName = "";
					this.labelValue.textFlow = card.attributeToString(this._attribute.name);
				} else {
					this.labelName.text = null;
					this.labelValue.text = null;
				}
			}
		}
		
		/**
		 * Cria um watcher para o valor do atributo
		 */
		protected function addValueWatcher () : void
		{
			if (this.valueWatcher != null && this._attribute != null) {
				this.valueWatcher.reset(this._attribute);
			} else if (this._attribute != null) {
				this.valueWatcher = ChangeWatcher.watch(this._attribute, "value", this.changeWatcherHandler );
			} else {
				this.valueWatcher.unwatch();
			}
		}
		
		/**
		 * Handler para mudança do valor de um atributo
		 * @param event
		 */
		private function changeWatcherHandler (event : Event) : void
		{
			this.refreshLabels();
		}
		
		/**
		 * Ao adicionar os labels, atualiza o texto deles
		 * @param partName
		 * @param instance
		 */
		override protected function partAdded(partName:String, instance:Object) : void
		{
			super.partAdded(partName, instance);
			this.refreshLabels();
		}
		
	}
	
}